home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc / sph_fall.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.9 KB  |  138 lines

  1. #include "HEADERS.h"
  2. /** FALLOC : falloc.c
  3. **/
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "falloc.h"
  9. #include "fallocdefs.h"
  10.  
  11. /** FALLOCnew_chunk
  12. Effects: Allocates a new chunk.
  13. **/
  14.  
  15. FALLOCchunk *
  16. FALLOCnew_chunk()
  17. {
  18.   register FALLOCchunk *chunk;
  19.  
  20.   /* These are tiny, so we can just fatal error if they fail */
  21.  
  22.   MALLOC_FATAL(chunk, FALLOCchunk, 1, "new chunk");
  23.   MALLOC_FATAL(chunk->blocks, char *, 1, "first blockptr in new chunk");
  24.   MALLOC_FATAL(chunk->over_blocks, char *, 1, "first blockptr in new chunk");
  25.  
  26.   chunk->magic         = MAGIC;
  27.   chunk->free_bytes     = 0;
  28.   chunk->cur_block     = -1;
  29.   chunk->num_blocks     = 0;
  30.   chunk->num_over_blocks = 0;
  31.  
  32.   return chunk;
  33. }
  34.  
  35. /** FALLOCalloc
  36. Effects: Allocs from the given chunk.
  37. **/
  38.  
  39. char *
  40. FALLOCalloc(chunk, nbytes, zero)
  41.   register FALLOCchunk *chunk;
  42.   register int nbytes;
  43.   int zero;
  44. {
  45.   register char *ret;
  46.   register int    cb = chunk->cur_block;
  47.   register int    nb = chunk->num_blocks;
  48.   register int    ob = chunk->num_over_blocks;
  49.  
  50.   assert (chunk->magic == MAGIC);
  51.  
  52.   nbytes = (nbytes + ALIGN_SIZE) & ALIGN_MASK;
  53.  
  54.   if (nbytes <= chunk->free_bytes) {
  55.     ret            = chunk->cur_ptr;
  56.     chunk->cur_ptr    += nbytes;
  57.     chunk->free_bytes -= nbytes;
  58.   }
  59.  
  60.   else if (nbytes > FALLOC_BLOCK_SIZE) {
  61.     REALLOC_RET(chunk->over_blocks, char *, ob + 1, (char *) NULL);
  62.     MALLOC_RET(chunk->over_blocks[ob], char, nbytes, (char *) NULL);
  63.     ret = chunk->over_blocks[ob];
  64.     chunk->num_over_blocks++;
  65.   }
  66.  
  67.   else {
  68.     if (++chunk->cur_block >= chunk->num_blocks) {
  69.       REALLOC_RET(chunk->blocks, char *, nb + 1, (char *) NULL);
  70.       MALLOC_RET(chunk->blocks[nb], char, FALLOC_BLOCK_SIZE, (char *) NULL);
  71.       chunk->num_blocks++;
  72.     }
  73.  
  74.     chunk->free_bytes = FALLOC_BLOCK_SIZE - nbytes;
  75.     ret           = chunk->blocks[cb + 1];
  76.     chunk->cur_ptr    = ret + nbytes;
  77.   }
  78.  
  79.   if (zero) memset(ret, 0, nbytes);  /* do this for turbo C  ADR  */
  80.  
  81. /*   deleted for turbo C  ADR
  82. #ifdef THINK_C
  83.   if (zero) memset(ret, 0, nbytes);
  84. #else
  85.   if (zero) bzero(ret, nbytes);
  86. #endif
  87. */
  88.  
  89.   return ret;
  90. }
  91.  
  92. /** FALLOCfree
  93. Effects: Frees a falloc chunk.
  94. **/
  95.  
  96. void
  97. FALLOCfree(chunk)
  98.   register FALLOCchunk *chunk;
  99. {
  100.   register int i;
  101.  
  102.   assert (chunk->magic == MAGIC);
  103.  
  104.   chunk->magic = ~MAGIC;
  105.  
  106.   for (i = 0; i < chunk->num_blocks; i++) FREE(chunk->blocks[i]);
  107.   for (i = 0; i < chunk->num_over_blocks; i++) FREE(chunk->over_blocks[i]);
  108.  
  109.   FREE(chunk->blocks);
  110.   FREE(chunk->over_blocks);
  111.   FREE(chunk);
  112. }
  113.  
  114.  
  115. /** FALLOCclear_chunk
  116. Effects: Clears but does not free a chunk.
  117.      It becomes reusable.
  118. **/
  119.  
  120. void
  121. FALLOCclear_chunk(chunk)
  122.   register FALLOCchunk *chunk;
  123. {
  124.   register int i;
  125.  
  126.   assert (chunk->magic == MAGIC);
  127.  
  128.   chunk->cur_block  = -1;
  129.   chunk->free_bytes = 0;
  130.  
  131.   for (i = 0; i < chunk->num_over_blocks; i++) FREE(chunk->over_blocks[i]);
  132.   chunk->num_over_blocks = 0;
  133.  
  134.   /* No need to free chunk->over_blocks, next realloc will take care of it */
  135. }
  136.  
  137.  
  138.